package cheqex.update;
import java.sql.Date;
import java.util.Calendar;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

import cheqex.update.controllers.*;
public class AddCollectionCashView extends JFrame implements ActionListener{
	
	private JTextField orNumber;
	private JTextField amount;
	private JTextField particular;
	private JPanel jp;
	
	private JButton save;
	private Calendar calendar;
	
	private CollectionView collectionView;
	
	public JPanel getCollectionView() {
		return collectionView;
	}

	public void setCollectionView(CollectionView collectionView) {
		this.collectionView = collectionView;
	}

	public AddCollectionCashView(CollectionView collectionView){
		this.collectionView = collectionView;
		initialize(this.collectionView);
	}
	
	//closes login frame
	private void closeframe(){
		this.setVisible(false);
	}
	
	public void initialize(CollectionView collectionView){
		orNumber = new JTextField(20);
		orNumber.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					addData();
				}
				
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});

		amount = new JTextField(20);
		amount.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					addData();
				}
				
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});

		particular = new JTextField(20);
		particular.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					addData();
				}
				
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		save = new JButton("Save");
		save.setMnemonic(KeyEvent.VK_D);
		save.setActionCommand("save");
		save.setEnabled(true);
		save.addActionListener(this);	
		
		JLabel orNo = new JLabel("OR Number:");
			orNo.setLabelFor(orNumber);
		JLabel amt = new JLabel("Amount:");
			amt.setLabelFor(amount);
		JLabel pr = new JLabel("Particular:");
			pr.setLabelFor(particular);
		
		jp = new JPanel(new FlowLayout());
		jp.add(orNo);
		jp.add(orNumber);
		jp.add(amt);
		jp.add(amount);
		jp.add(pr);
		jp.add(particular);
		jp.add(save);
		
		this.add(jp);
		this.setResizable(false);
		this.pack();
		this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		this.setTitle("Adding");
	}
	
	private void addData(){
		int amt = -9999;
		boolean err = true;
		int orLength = orNumber.getText().length();
		if(orLength!=0){
			if(orLength>20){
				err = false;
				JOptionPane.showMessageDialog(null,"OR number is too long.", "Error", JOptionPane.WARNING_MESSAGE);
			}
			else
			if (!CollectionController.isNumeric(orNumber.getText())){
				err = false;
				JOptionPane.showMessageDialog(null,"OR number should only consists of digits.", "Error", JOptionPane.WARNING_MESSAGE);
			}
		} 
		
		
		int amtLength=amount.getText().length();
		if(amtLength!=0){
			if(amtLength>15){
				err = false;
				JOptionPane.showMessageDialog(null,"Amount is too long.", "Error", JOptionPane.WARNING_MESSAGE);
			}
			else
			if(!CollectionController.isNumeric(amount.getText())){
				err = false;
				JOptionPane.showMessageDialog(null, "The amount should only consists of digits.", "Error", JOptionPane.WARNING_MESSAGE);
			}else{ 
				amt = Integer.parseInt(amount.getText());
				if(amt==0){
					err = false;
					JOptionPane.showMessageDialog(null, "The amount should not be equal to zero.", "Error", JOptionPane.WARNING_MESSAGE);
				}
			}
		}
		
		if(err){
			String part = particular.getText();
			java.sql.Date date = DateController.getDate();
			
			int id = CollectionController.validateDataToBeAdded(orNumber.getText(), amt, part);
			switch(id){
			case 1:	JOptionPane.showMessageDialog(null, 
					"No OR number entered.", "Error", JOptionPane.WARNING_MESSAGE);	
					break;		
			case 2:	JOptionPane.showMessageDialog(null, 
					"No OR number and amount entered.", "Error", JOptionPane.WARNING_MESSAGE);	
					break;
			case 3:	JOptionPane.showMessageDialog(null, 
					"No OR number, amount, and particular entered.", "Error", JOptionPane.WARNING_MESSAGE);	
					break;
			case 4:	JOptionPane.showMessageDialog(null, 
					"No OR number and particular entered.", "Error", JOptionPane.WARNING_MESSAGE);	
					break;
			case 5:	JOptionPane.showMessageDialog(null, 
					"No amount and particular entered.", "Error", JOptionPane.WARNING_MESSAGE);	
					break;
			case 6:	JOptionPane.showMessageDialog(null, 
					"No amount entered.", "Error", JOptionPane.WARNING_MESSAGE);	
					break;
			case 7:	JOptionPane.showMessageDialog(null, 
					"No particular entered.", "Error", JOptionPane.WARNING_MESSAGE);	
					break;		
			
			case 8: int y = DateController.getYear();
					int m = DateController.getMonth();
					int d = DateController.getDay();
					//System.out.printf("%d %d %d",y,m,d);
					calendar = Calendar.getInstance();
					//calendar.clear();
					calendar.set(y, m-1, d);
					String weekId = calendar.get(Calendar.YEAR)+""+calendar.get(Calendar.WEEK_OF_YEAR);
					//System.out.println(""+calendar.get(Calendar.WEEK_OF_YEAR));
					id = Integer.parseInt(weekId);
				
					try{		
						if(CollectionController.addCollectionCash(orNumber.getText(), date, amt, part, id)){
							if(ActivitiesController.addActivity(DateController.getToday(), "Data added in collection cash table.")){
								JOptionPane.showMessageDialog(null,"Data Added","Successful",JOptionPane.DEFAULT_OPTION);
							} else JOptionPane.showMessageDialog(null,"An error happened.","Error",JOptionPane.DEFAULT_OPTION);
						}
						else JOptionPane.showMessageDialog(null,"Duplicate OR number entry","Error",JOptionPane.DEFAULT_OPTION);
						orNumber.setText(null);
						amount.setText(null);
						particular.setText(null);
						this.collectionView.populateTable();
						closeframe();
					} 
					catch (Exception a){a.printStackTrace();}
					break;
						
			}			
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if ("save".equals(e.getActionCommand())) {
				addData();
		}
	}		
}

